<?php
class Controller_Appointment extends Controller_Template
{
	//Constructor
	public function __construct()
	{
		parent::__construct();
		$this->selfModel = new Model_Appointment();
		$this->profileModel = new Model_Profile();
		$this->personModel = new Model_Person();
		$this->carModel = new Model_Car();
		$this->notificationModel = new Model_Notification();
		$this->procedureModel = new Model_Procedure();
		$this->inventoryModel = new Model_Inventory();
		$this->newDate = 0;
	}
	
	//Methods
	public function index()
	{
		$title = "Appointment";
		$requiredField = '<span class="red">*</span> Required field</br>';
		$date = date("Y-m-d");
		$date = self::setMondayDate($date);
		
		$allAppointments = $this->selfModel->getOneWeekAppointments($date);
		
		require 'View/header.tpl';
		require 'View/menu.tpl';
		require 'View/appointment/index.tpl';
		require 'View/footer.tpl';
		//return true;
	}
	public function search($phone, $date, $lastURI)
	{
		$title = "Appointment Search";
		$requiredField = '<span class="red">*</span> Required field</br>';
		
		$person = $this->personModel->getPerson($phone);
		if (empty($person))
		{
			if (isset($_SESSION['searchProfilePhone']))
			{
				unset($_SESSION['searchProfilePhone']);
			}
			setcookie("SearchAppointment","PersonNotExist");
			header('Location: '.html($lastURI));
		}
		else
		{
			$profile = $this->profileModel->getProfile($person->getPersonId());
			if (empty($profile))
			{
				if (isset($_SESSION['searchProfilePhone']))
				{
					unset($_SESSION['searchProfilePhone']);
				}
				setcookie("SearchAppointment","ProfileNotExist");
				header('Location: '.html($lastURI));
			}
			else
			{
				$cars = $this->carModel->getAllCars($profile->getProfileId());
				$notifications=$this->notificationModel->getAllNotifications($profile->getProfileId());
				$notificationType=$this->notificationModel->getAllNotificationTypes();
				$appointments = $this->selfModel->getAllAppointmentsByProfileId($profile->getProfileId());
				$procedures = $this->procedureModel->getAllProcedures();
				$allProceduresItems = $this->procedureModel->getAllProcedureItems();
				$inventoryItems = $this->inventoryModel->getAllInventoryItems();
				$allAppointments = $this->selfModel->getOneWeekAppointments($date);
				
				$date = self::setMondayDate($date);
				
				require 'View/header.tpl';
				require 'View/menu.tpl';
				require 'View/appointment/index.tpl';
				require 'View/footer.tpl';
			}
		}
		
		//return false;
	}
	public function create($clientId, $date, $appointmentId)
	{
		$title = "Appointment Create";
		$requiredField = '<span class="red">*</span> Required field</br>';
	
		$profile = $this->profileModel->getProfileById($clientId);
		if (empty($profile))
		{
			return;
		}
		else
		{
			$person = $this->personModel->getPersonById($profile->getPersonId());
			if (empty($person))
			{
				return;
			}
			else
			{
				$cars = $this->carModel->getAllCars($profile->getProfileId());
				$notifications=$this->notificationModel->getAllNotifications($profile->getProfileId());
				$notificationType=$this->notificationModel->getAllNotificationTypes();
				$appointments = $this->selfModel->getAllAppointmentsByProfileId($profile->getProfileId());
				$procedures = $this->procedureModel->getAllProcedures();
				$allProceduresItems = $this->procedureModel->getAllProcedureItems();
				$inventoryItems = $this->inventoryModel->getAllInventoryItems();
				$proceduresByAppointment = $this->selfModel->getProceduresByAppointment($appointmentId);
				$allAppointments = $this->selfModel->getOneWeekAppointments($date);
				$selectedAppointment = $this->selfModel->getAppointmentById($appointmentId);
			}
		}
		
		$date = self::setMondayDate($date);
	
		require 'View/header.tpl';
		require 'View/menu.tpl';
		require 'View/appointment/index.tpl';
		require 'View/footer.tpl';
		//return true;
	}
	public function selectAppointments($id, $date)
	{
	 
		$title = "Appointment Selection";
		$requiredField = '<span class="red">*</span> Required field</br>';
		
		$selectedAppointment = $this->selfModel->getAppointmentById($id);
		if (empty($selectedAppointment))
		{
			return;
		}
		else
		{	
			$profile = $this->profileModel->getProfileById($selectedAppointment->getProfileId());
			if (empty($profile))
			{
				return;
			}
			else
			{
				$person = $this->personModel->getPersonById($profile->getPersonId());
				if (empty($person))
				{
					return;
				}
				else
				{
					$cars = $this->carModel->getAllCars($profile->getProfileId());
					$notifications=$this->notificationModel->getAllNotifications($profile->getProfileId());
					$notificationType=$this->notificationModel->getAllNotificationTypes();
					$appointments = $this->selfModel->getAllAppointmentsByProfileId($profile->getProfileId());
					$procedures = $this->procedureModel->getAllProcedures();
					$allProceduresItems = $this->procedureModel->getAllProcedureItems();
					$inventoryItems = $this->inventoryModel->getAllInventoryItems();
					$proceduresByAppointment = $this->selfModel->getProceduresByAppointment($id);
					$allAppointments = $this->selfModel->getOneWeekAppointments($date);
				}
			}
		}
	
		$date = self::setMondayDate($date);
	
		require 'View/header.tpl';
		require 'View/menu.tpl';
		require 'View/appointment/index.tpl';
		require 'View/footer.tpl';
		//return false;
	}
	public function selectCars($index, $phone, $date)
	{
		$title = "Appointment Cars";
		$requiredField = '<span class="red">*</span> Required field</br>';
	
		$carIndex = $index;
		$person = $this->personModel->getPerson($phone);
		if (empty($person))
		{
			return;
		}
		else
		{
			$profile = $this->profileModel->getProfile($person->getPersonId());
			if (empty($profile))
			{
				return;
			}
			else
			{
				$cars = $this->carModel->getAllCars($profile->getProfileId());
				$notifications=$this->notificationModel->getAllNotifications($profile->getProfileId());
				$notificationType=$this->notificationModel->getAllNotificationTypes();
				$appointments = $this->selfModel->getAllAppointmentsByProfileId($profile->getProfileId());
				$procedures = $this->procedureModel->getAllProcedures();
				$allProceduresItems = $this->procedureModel->getAllProcedureItems();
				$inventoryItems = $this->inventoryModel->getAllInventoryItems();
				$allAppointments = $this->selfModel->getOneWeekAppointments($date);
			}
		}
		
		$date = self::setMondayDate($date);
	
		require 'View/header.tpl';
		require 'View/menu.tpl';
		require 'View/appointment/index.tpl';
		require 'View/footer.tpl';
		//return true;
	}
	public function displayWeeklyAppointments($newDate)
	{
		$title = "Weekly Appointments";
		$requiredField = '<span class="red">*</span> Required field</br>';
	
		$allAppointments = $this->selfModel->getOneWeekAppointments($newDate);
		$date = $newDate;
	
		require 'View/header.tpl';
		require 'View/menu.tpl';
		require 'View/appointment/index.tpl';
		require 'View/footer.tpl';
		//return false;
	}
	
	private function setMondayDate($date)
	{
		$md=strtotime($date);
		
		while(date('D',$md)!='Mon')//////loop untill monday
		{
			$md-=60*60*24;/////take off one day
		}
		
		return date('Y-m-d', $md);
	}
}